package com.md.application.assembler;

import com.md.base.exception.ServerException;
import com.md.domain.model.bo.QueryUsersBO;
import com.md.domain.model.po.UsersPO;
import com.md.request.QueryUsersPageRequest;
import com.md.request.RegisterUsersRequest;
import com.md.response.RoleResponse;
import com.md.response.UsersDetailResponse;
import com.md.response.UsersResponse;
import com.md.response.open.SimpleUsersResponse;
import com.md.util.ClassUtils;
import com.md.util.MatchUtil;
import com.md.util.ObjectUtil;
import com.md.util.PinYinUtil;
import com.md.util.StringUtil;
import com.md.value.SexEnum;
import com.md.value.UsersOrderEnum;
import com.md.value.UsersStatusEnum;
import com.md.value.enums.ErrorEnum;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.Optional;

import static com.md.infrastructure.value.ErrorEnum.REGISTER_INFO_ILLEGAL;

/**
 * @author 刘骄阳
 * 2024-12-15 1:49
 */
@UtilityClass
public class UsersAssembler {
  public static void checkRegisterUserRequest(final RegisterUsersRequest request) {
    if (ObjectUtil.isEmpty(request.getRegisterType())) {
      throw ServerException.build(ErrorEnum.IS_EMPTY, "注册类型");
    }

    switch (request.getRegisterType()) {
      case EMAIL:
        if (ObjectUtil.isEmpty(request.getEmail())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "邮箱地址");
        }
        if (ObjectUtil.isEmpty(request.getEmailType())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "邮箱类型");
        }
        if (!MatchUtil.isEmail(request.getEmail())) {
          throw ServerException.build(REGISTER_INFO_ILLEGAL, "邮箱地址");
        }
        break;
      case PHONE:
        if (ObjectUtil.isEmpty(request.getPhone())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "手机号");
        }
        if (!MatchUtil.isPhone(request.getPhone())) {
          throw ServerException.build(REGISTER_INFO_ILLEGAL, "手机号");
        }
        break;
      case AUTH_CODE:
        if (ObjectUtil.isEmpty(request.getAuthCode())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "身份证");
        }
        if (!MatchUtil.isAuthCode(request.getAuthCode())) {
          throw ServerException.build(REGISTER_INFO_ILLEGAL, "身份证");
        }
        break;
      default:
        break;
    }
    if (ObjectUtil.isEmpty(request.getPassword())) {
      throw ServerException.build(ErrorEnum.IS_EMPTY, "密码");
    }
  }

  public static UsersPO buildRegisterUsersPO(final RegisterUsersRequest request) {
    return ClassUtils.source2Target(request, UsersPO.class, usersPO -> {
      usersPO.setFirstLabel(PinYinUtil.getPinYinHeadChar(request.getName()));
      usersPO.setSex(request.getSex().getCode());
      usersPO.setStatus(UsersStatusEnum.NOT_ACTIVE.getCode());
    });
  }

  public static UsersResponse buildUsers2Response(final UsersPO users) {
    return ClassUtils.source2Target(users, UsersResponse.class, usersPO -> {
      usersPO.setAuthCode(StringUtil.maskString(usersPO.getAuthCode()));
      usersPO.setSex(SexEnum.of(users.getSex()));
      usersPO.setPhone(StringUtil.maskString(usersPO.getPhone()));
      usersPO.setStatus(UsersStatusEnum.of(users.getStatus()));
    });
  }

  public static SimpleUsersResponse buildSimpleUsersResponse(final UsersPO users) {
    return ClassUtils.source2Target(users, SimpleUsersResponse.class);
  }

  public static UsersDetailResponse buildUsers2DetailResponse(final UsersPO users,
                                                              final List<RoleResponse> userRoleResponseList) {
    return ClassUtils.source2Target(users, UsersDetailResponse.class, usersDetailResponse -> {
//      usersDetailResponse.setAuthCode(StringUtil.maskString(users.getAuthCode()));
      usersDetailResponse.setSex(SexEnum.of(users.getSex()));
      usersDetailResponse.setPhone(StringUtil.maskString(users.getPhone()));
      usersDetailResponse.setStatus(UsersStatusEnum.of(users.getStatus()));
      usersDetailResponse.setUsersRoleList(userRoleResponseList);
    });
  }

  public static QueryUsersBO buildQueryUsersBO(final QueryUsersPageRequest request) {
    return ClassUtils.source2Target(request, QueryUsersBO.class, queryBO -> {
      Optional.ofNullable(request.getOrder()).map(UsersOrderEnum::getColumn).ifPresent(queryBO::setOrderColum);
      Optional.ofNullable(request.getStatus()).map(UsersStatusEnum::getCode).ifPresent(queryBO::setStatus);
      Optional.ofNullable(request.getSex()).map(com.md.value.SexEnum::getCode).ifPresent(queryBO::setSex);
    });
  }
}
